﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace 建设项目数据爬取
{
    /// <summary>
    /// AddNewCity.xaml 的交互逻辑
    /// </summary>
    public partial class AddNewCity : Window
    {
        public string cityName;
        public string url;
        public string indexXpath;
        public string detailTitleXpath;
        public string detailPublishTimeXpath;
        public AddNewCity()
        {
            InitializeComponent();
        }

        private void AddXpaths(object sender, RoutedEventArgs e)
        {
            cityName = cityNameTextbox.Text;
            url = indexUrlTextbox.Text;
            indexXpath = indexPathTextbox.Text;
            detailTitleXpath = detailTitleTextbox.Text;
            detailPublishTimeXpath = publishTimeTextbox.Text;
            saveToDatabase();
            XpathIsAdded?.Invoke();
            this.Close();
        }

        void saveToDatabase()
        {
            DatabaseTool.GetInstance().OpenDatabase("xmjs");
            string insertSQL = string.Format("INSERT IGNORE INTO Xpaths(cityName,url,indexXpath,detailTitleXpath,publishTimeXpath) values('{0}','{1}','{2}','{3}','{4}')", cityName, url, indexXpath, detailTitleXpath, detailPublishTimeXpath);
            MySqlCommand command2 = new MySqlCommand(insertSQL, DatabaseTool.connection);
            int count2 = command2.ExecuteNonQuery();
            if (count2 == 0)
                Debug.WriteLine("Xpaths数据保存成功!");
            DatabaseTool.GetInstance().CloseDatabase("xmjs");
        }

        public event Action XpathIsAdded;
    }
}
